﻿using MediatR;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Domain.DTO.ReportlossesDto;
using W.Warehouse.Domain.Reportlosses;
using W.Warehouse.Read.Api.Application.Command.ReportlossesCommand;
using W.Warehouse.Repository.Impl;
using W.Warehouse.Repository.Interface;

namespace W.Warehouse.Read.Api.Application.CommandHandlder.ReportlossesCommandHandler
{
    public class ReportlossesDetailsCommandHandler : IRequestHandler<ReportlossesDetailsCommand, ReportlossesDto>
    {
        private readonly IReportlossesRepository reportlossesRepository;
        private readonly IReportProductRepository reportproductRepository;

        public ReportlossesDetailsCommandHandler(IReportlossesRepository reportlossesRepository,
            IReportProductRepository reportproductRepository)
        {
            this.reportlossesRepository = reportlossesRepository;
            this.reportproductRepository = reportproductRepository;

        }
        /// <summary>
        /// 报损查看详情
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ReportlossesDto> Handle(ReportlossesDetailsCommand request, CancellationToken cancellationToken)
        {

            var products = await reportproductRepository.GetAllAsync();
            var list = from a in await reportlossesRepository.GetAllAsync()
                        where a.IsDelete == false && a.ReportlossesId == request.ReportlossesId
                        select new ReportlossesDto
                        {
                            ReportlossesId = a.ReportlossesId,
                            ReportlossesNo = a.ReportlossesNo,
                            ReportlossesType = a.ReportlossesType,
                            AssociateNumber = a.AssociateNumber,
                            SingleMaker = a.SingleMaker,
                            MakeTime = a.MakeTime,
                            MoveRemark = a.MoveRemark,
                            DocumentState = a.DocumentState,
                            ExamineReason = a.ExamineReason,
                            ExaminePeople=a.ExaminePeople,
                            ExamineTime=a.ExamineTime,
                            officeId=a.officeId,
                            userid=a.userid,
                            Wlocator=a.Wlocator,
                            WRepolocator = a.WRepolocator,
                            CreateName=a.CreateName,
                            CreateTime=a.CreateTime,
                            DeleteName=a.DeleteName,
                            DeleteTime=a.DeleteTime,
                            UpdateTime=a.UpdateTime,
                            UpdateName=a.UpdateName,
                            ReportLists = products.Where(x => x.ReportlossesId == a.ReportlossesId).Select(x => new ReportList
                            {
                                ReportProductId=x.ReportProductId,
                                ReportlossesId=x.ReportlossesId,
                                ProductNo = x.ProductNo,
                                ProductName = x.ProductName,
                                ProductSpec = x.ProductSpec,
                                Price=x.Price,
                                Batch=x.Batch,
                                ReportNum=x.ReportNum,
                                BinLocationName=x.BinLocationName,
                                officeId = x.officeId,
                                userid = x.userid,
                                CreateName = a.CreateName,
                                CreateTime = a.CreateTime,
                                DeleteName = a.DeleteName,
                                DeleteTime = a.DeleteTime,
                                UpdateTime = a.UpdateTime,
                                UpdateName = a.UpdateName,
                            }).ToList()

                        };
            return list.FirstOrDefault();
                        
        }
    }
}
